﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NanoERP.Business.Permission;
using NanoERP.Model.Poco;
using NanoERP.Infrastructure;

namespace NanoERP.Modules.SystemMgmt
{
    //[Module("UserMgmt")]
    public partial class FrmEditModule : EditFormBase
    {
        private SYS_User _data;

        public FrmEditModule()
        {
            InitializeComponent();
        }

        public FrmEditModule(SYS_User user)
        {
            Data = user;
        }

        public SYS_User Data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
                this.Mode = this.Enabled ? FormMode.Edit : FormMode.View;
            }
        }

        protected override void LoadData()
        {
            this.Id = Data.Id;
            this.longinNameTextBox.Text = Data.LoginName;
            this.descriptionTextBox.Text = Data.Remark;
            this.nameTextBox.Text = Data.Name;
            this.sortTextBox.Text = Data.Sort.ToString();
        }

        protected override bool SaveData()
        {
            var srv = new UserService();

            SYS_User user = !this.Id.Equals(Guid.Empty)
                                ? srv.Get(this.Id)
                                : new SYS_User();

            user.LoginName = this.longinNameTextBox.Text;
            user.Remark = this.descriptionTextBox.Text;
            user.Name = this.nameTextBox.Text;
            user.Sort = Int32.Parse(this.sortTextBox.Text);

            if (this.Mode == FormMode.Add)
            {
                srv.Add(user);
            }
            else
            {
                srv.Update(user);
            }
            return true;
        }

    }
}
